package com.campus.service.impl;


import com.campus.entry.SysUser;
import com.campus.exception.BusinessException;
import com.campus.pojo.dto.LoginUser;
import com.campus.response.ResultCode;
import com.campus.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *
 * 根据登录的用户名从数据库中查询数据
 * @author 荧moon
 * @date 2024/12/26
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService sysUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user=sysUserService.getSysUserByName(username);
        if(Objects.isNull(user)) {
            throw new BusinessException(ResultCode.NO_REGISTER);
        }
        return new LoginUser(user);
    }

}
